package com.bluefox.hospital.controller;

import com.bluefox.common.web.dto.result.PageResultDTO;
import com.bluefox.common.web.dto.result.ResultDTO;
import com.bluefox.hospital.convertor.AnnouncementConvertor;
import com.bluefox.hospital.dto.AnnouncementDTO;
import com.bluefox.hospital.model.Announcement;
import com.bluefox.hospital.model.RichText;
import com.bluefox.hospital.repository.AnnouncementRepository;
import com.bluefox.hospital.service.AnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Announcement的管理接口
 *
 * @author
 */
 @Slf4j
@RestController
@RequestMapping("/w/announcements")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;
	@Autowired
    private AnnouncementRepository announcementRepository;
    @Autowired
    private AnnouncementConvertor announcementConvertor;

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @return 分页数据
     */
    @RequestMapping(value = "/s", method = RequestMethod.POST)
    public PageResultDTO<AnnouncementDTO> search(@RequestParam(value = "type", required = false) String type, final Pageable pageable) {
        Pageable request = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort().and(Sort.by(Sort.Order.desc("createdDate"))));

        Page<Announcement> models=null;
        if(!StringUtils.isEmpty(type)){
            models = this.announcementService.findAll(type, request);
        }else{
            models = this.announcementService.findAll( request);
        }

        return this.announcementConvertor.toResultDTO(models);
    }
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public PageResultDTO<AnnouncementDTO> searchWeb(@RequestParam(value = "type", required = false) String type, final Pageable pageable) {
        Pageable request = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort().and(Sort.by(Sort.Order.desc("createdDate"))));
        Page<Announcement> models=null;
        if(!StringUtils.isEmpty(type)){
            models = this.announcementService.findAllWeb(type, request);
        }else{
            models = this.announcementService.findAllWeb( request);
        }

        return this.announcementConvertor.toResultDTO(models);
    }
    /**
     * 取得详细数据
     *
     * @param id 资源ID
     * @return 资源详细
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<AnnouncementDTO> get(@PathVariable final Long id) {
        final Announcement model = this.announcementService.get(id);
        return this.announcementConvertor.toResultDTO(model);
    }

    /**
     * 新建操作
     *
     * @param announcementDTO 新建资源的DTO
     * @return 新建资源
     */

    @RequestMapping(method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<AnnouncementDTO> create(@RequestBody @Valid final AnnouncementDTO announcementDTO) {
        final Announcement model = this.announcementConvertor.toModel(announcementDTO);
        this.announcementService.create(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was created.", Announcement.class.getSimpleName(), model.getId());
        }
        return this.announcementConvertor.toResultDTO(model);
    }

    /**
     * 更新操作
     *
     * @param id 更新资源的ID
     * @param announcementDTO 更新资源的DTO
     * @return 更新后资源
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<AnnouncementDTO> update(@PathVariable final Long id, @RequestBody @Valid final AnnouncementDTO announcementDTO) {
        announcementDTO.setId(id);
        final Announcement model = this.announcementConvertor.toModel(announcementDTO);
        this.announcementService.update(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", Announcement.class.getSimpleName(), model.getId());
        }
        return this.announcementConvertor.toResultDTO(model);
    }
    /**
     * 置顶操作
     *
     * @return 更新后资源
     */
    @RequestMapping(value = "/roofPlacement/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<AnnouncementDTO> roofPlacement(@PathVariable final Long id) {
        Announcement model = this.announcementService.get(id);
        this.announcementService.roofPlacement(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", RichText.class.getSimpleName(), model.getId());
        }
        return this.announcementConvertor.toResultDTO(model);
    }

    /**
     * 删除操作
     *
     * @param Id 资源ID
     * @return 操作结果
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.announcementService.delete(id);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was deleted.", Announcement.class.getSimpleName(), id);
        }
        return ResultDTO.success();
    }
    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @param type     查询类型 过滤未通过
     * @return 分页数据
     */
    @RequestMapping(value = "/findByTypeStatus", method = RequestMethod.POST)
    public PageResultDTO<AnnouncementDTO> findByTypeStatus(@RequestParam(value = "type", required = false) String type, final Pageable pageable) {
        Pageable request = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort().and(Sort.by(Sort.Order.desc("createdDate"))));
        final Page<Announcement> models = this.announcementService.findByTypeAndStatus(type,1,request);
        return this.announcementConvertor.toResultDTO(models);
    }
}
